/**
 * Created by Administrator on 2019/10/5.
 */
$(function () {

    $("#searchDbInfoBtn").click(function () {
        var typeName = I18n.system_refresh;
        $.ajax({
            type: 'POST',
            url: base_url + "/datacenter/searchDataInfo",
            data: {
                "dbType": $(".content select[name='dbType']").val()
            },
            dataType: "json",
            success: function (data) {
                console.log(data);
                if (data.code == 200) {
                    $(".content select[name='dbinfo']").html("");
                    var content = data.content;
                    for(var i=0; i<content.length; i++) {
                        var ele = content[i];
                        $(".content select[name='dbinfo']").append("<option value='"+ele.id+"'>"+ele.name+"</option>");
                    }
                    $(".content select[name='tablename']").html("");
                } else {
                    layer.msg( data.msg || typeName + I18n.system_fail );
                }
            }
        });
    });

    $("#searchTableNameBtn").click(function () {
        var typeName = I18n.system_refresh;
        $.ajax({
            type: 'POST',
            url: base_url + "/datacenter/searchTableName",
            data: {
                "dbType": $(".content select[name='dbType']").val(),
                "dbInfo": $(".content select[name='dbinfo']").val()
            },
            dataType: "json",
            success: function (data) {
                console.log(data);
                if (data.code == 200) {
                    $(".content select[name='tablename']").html("");
                    var content = data.content;
                    for(var i=0; i<content.length; i++) {
                        var ele = content[i];
                        $(".content select[name='tablename']").append("<option value='"+ele.table_name+"'>"+ele.table_comment+"</option>");
                    }
                } else {
                    layer.msg( data.msg || typeName + I18n.system_fail );
                }
            }
        });
    });

    $("#exportBtn").on('click', function () {
        var typeName = I18n.system_export;
        var sheetName = $(".content select[name='tablename'] :selected").text();
        var dbinfo = $(".content select[name='dbinfo']").val();
        var tableName = $(".content select[name='tablename']").val();
        if(dbinfo == undefined || dbinfo == '' || tableName == undefined || tableName == '') {
            layer.msg( typeName + I18n.system_fail );
            return;
        }
        $.ajax({
            type: 'POST',
            url: base_url + "/datacenter/export",
            data: {
                "dbInfo": dbinfo,
                "tableName" : tableName,
                "sheetName": sheetName
            },
            dataType: "json",
            success: function (data) {
                if (data.code == 200) {
                    $("input[name='filePath']:hidden").val(data.content);
                    layer.msg( typeName + I18n.system_success );
                } else {
                    layer.msg( data.msg || typeName + I18n.system_fail );
                }
            }
        });
    });

    $("#searchBtn").on('click', function () {
        var typeName = I18n.system_search;
        var dbinfo = $(".content select[name='dbinfo']").val();
        var tableName = $(".content select[name='tablename']").val();
        if(dbinfo == undefined || dbinfo == '' || tableName == undefined || tableName == '') {
            layer.msg( typeName + I18n.system_fail );
            return;
        }
        $.ajax({
            type: 'POST',
            url: base_url + "/datacenter/searchTableFields",
            data: {
                "dbInfo": dbinfo,
                "tableName" : tableName
            },
            dataType: "json",
            success: function (data) {
                console.log(data);
                if (data.code == 200) {
                    layer.msg( typeName + I18n.system_success );

                    var columns = [];
                    $("#datacenter_list thead tr").html("");
                    var content = data.content;
                    for(var i=0; i<content.length; i++) {
                        var field = {
                            "data": content[i].field,
                            "bSortable": false,
                            "visible" : true
                        };
                        columns.push(field);
                        $("#datacenter_list thead tr").append("<th name='"+content[i].field+"'>"+content[i].comment+"</th>");
                    }

                    $("#datacenter_list").dataTable({
                        "deferRender": true,
                        "processing": true,
                        "serverSide": true,
                        "ajax": {
                            url: base_url + "/datacenter/pageList",
                            type: "post",
                            data: function (d) {
                                var obj = {};
                                obj.dbInfo = dbinfo;
                                obj.tableName = tableName;
                                obj.start = d.start;
                                obj.length = d.length;
                                return obj;
                            }
                        },
                        "searching": false,
                        "ordering": false,
                        //"scrollX": true,	// scroll x，close self-adaption
                        "columns": columns,
                        "language": {
                            "sProcessing": I18n.dataTable_sProcessing,
                            "sLengthMenu": I18n.dataTable_sLengthMenu,
                            "sZeroRecords": I18n.dataTable_sZeroRecords,
                            "sInfo": I18n.dataTable_sInfo,
                            "sInfoEmpty": I18n.dataTable_sInfoEmpty,
                            "sInfoFiltered": I18n.dataTable_sInfoFiltered,
                            "sInfoPostFix": "",
                            "sSearch": I18n.dataTable_sSearch,
                            "sUrl": "",
                            "sEmptyTable": I18n.dataTable_sEmptyTable,
                            "sLoadingRecords": I18n.dataTable_sLoadingRecords,
                            "sInfoThousands": ",",
                            "oPaginate": {
                                "sFirst": I18n.dataTable_sFirst,
                                "sPrevious": I18n.dataTable_sPrevious,
                                "sNext": I18n.dataTable_sNext,
                                "sLast": I18n.dataTable_sLast
                            },
                            "oAria": {
                                "sSortAscending": I18n.dataTable_sSortAscending,
                                "sSortDescending": I18n.dataTable_sSortDescending
                            }
                        }
                    });

                } else {
                    layer.msg( data.msg || typeName + I18n.system_fail );
                }
            }
        });
    });

    $("#openDirectoryBtn").on('click', function () {
        var typeName = I18n.system_open_directory;
        var filePath = $("input[name='filePath']:hidden").val();
        console.log(filePath);
        if(filePath == undefined || filePath == '') {
            layer.msg( typeName + I18n.system_fail );
            return;
        }
        $.ajax({
            type: 'POST',
            url: base_url + "/datacenter/openDirectory",
            data: {
                "filePath": filePath
            },
            dataType: "json",
            success: function (data) {
                if (data.code != 200) {
                    layer.msg( data.msg || typeName + I18n.system_fail );
                }
            }
        });
    });

    $("#openBtn").on('click', function () {
        var typeName = I18n.system_open;
        var filePath = $("input[name='filePath']:hidden").val();
        console.log(filePath);
        if(filePath == undefined || filePath == '') {
            layer.msg( typeName + I18n.system_fail );
            return;
        }
        $.ajax({
            type: 'POST',
            url: base_url + "/datacenter/open",
            data: {
                "filePath": filePath
            },
            dataType: "json",
            success: function (data) {
                if (data.code != 200) {
                    layer.msg( data.msg || typeName + I18n.system_fail );
                }
            }
        });
    });
});